package com.fz.us.oa.web.shiro.filter;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.servlet.AdviceFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: xuzhou
 * Date: 2015/12/11
 * Time: 11:20
 */
public class SysSessionFilter extends AdviceFilter {

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        if (userName == null) {
            PrintWriter out = response.getWriter();
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("<script type=\"text/javascript\">");
            stringBuffer.append("window.location.href=\"login\"");
            stringBuffer.append("</script>");
            out.print(stringBuffer.toString());
            return false;
        }
        return super.preHandle(request, response);
    }

    @Override
    protected void postHandle(ServletRequest request, ServletResponse response) throws Exception {
        super.postHandle(request, response);
    }

}
